package com.happyclub.paul;

import com.happyclub.paul.service.IUserService;
import com.happyclub.paul.service.PersonalPhotoAlbumService;
import com.happyclub.pojo.TbPersonalPhotoAlbum;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;



    /**
     * 返回至登录页面
     * @return
     */
    @RequestMapping("login")
    public String userlogin(){
        return "paul-html/userlogin";
    }


    /**
     *
     * 功能描述: 根据账号和密码判断用户是否存在或是否注册
     * @auther: 魏泉琦
     * @date:
     */
    @RequestMapping(value = "queryUserExists")
    public String  queryUserExists(Model model,@RequestParam("telephone") String telephone, @RequestParam("userPassword") String userPassword){
        //查询账号密码
        JsonResult jsonResult = userService.queryUserExists(telephone, userPassword);
        model.addAttribute("jsonResult",jsonResult);
        if("500".equals(jsonResult.getErrorCode())){         //用户未注册，回到登录页面，提示要注册
            return "paul-html/userlogin";
        }
        if("501".equals(jsonResult.getErrorCode())){         //用户密码错误，回到登录页面，提示密码错误
            return "paul-html/userlogin";
        }
        if("400".equals(jsonResult.getErrorCode())){         //异常回到错误页面
            return "error/5XX";
        }
        return "paul-html/loginsuccess";                     //用户存在，登录成功
    }

    /**
     *@Description: 用户点击注册，回到注册页面让用户注册
     *@Author: 魏泉琦
     *@date: 2019/4/3
     */
    @RequestMapping(value = "/userregister")
    public String userRegister(){
        return "paul-html/userregister";
    }


    /**
     *@Description: 保存注册用户信息
     *@Author: 魏泉琦
     *@date: 2019/4/3
     */
    @RequestMapping(value = "/saveUserRegister")
    public String saveUserRegister(TbUser user){
        System.out.println("用户所在城市:"+user.getCity());
        JsonResult jsonResult = userService.insertUser(user);
        if("NO".equals(jsonResult.getErrorCode())){
            return "paul-html/userregister";
        }
        if("error".equals(jsonResult.getErrorCode())){
            return "error/5XX";
        }
        //注册成功
        return "paul-html/registersuccess";
    }

    /**
     *@Description: 用户点击个人信息  展示该用户的全部信息
     *@Author: 魏泉琦
     *@date: 2019/4/3
     */
    @RequestMapping(value = "showUserALlMsg")
    public String showUserALlMsg(){
        return "paul-html/usermsg";
    }


    /**
     *@Description: 点击修改回显用户信息
     *@Author: 魏泉琦
     *@date: 2019/4/3
     */
    @ResponseBody
    @RequestMapping(value = "echoUsrMsg")
    public JsonResult echoUsrMsg(HttpServletRequest request){
        HttpSession session = request.getSession();
       /*
       User user =(User) session.getAttribute("user");
       return userService.queryUserById(user.getUserId());*/
        //测试
        return userService.queryUserById("c965a37f-5752-4837-9735-e4a49db6915e");
    }


   /* @ResponseBody
    @RequestMapping(value = "")
    public JsonResult saveUpdateUserMsg(){

    }*/


    /**
     *@Description: 用户点击相册  查询出该用户的所有相册，然后跳转
     *@Author: 魏泉琦
     *@date: 2019/4/6
     */
    @GetMapping(value = "showUserPhotoAlbum")
    public String  showUserPhotoAlbum(Model model,HttpServletRequest request){
      /*
       HttpSession session = request.getSession();
       TbUser user =(TbUser) session.getAttribute("user");
       String userId = user.getUserId();
       */

        //测试代码
        String userId ="97fd7504-2253-4ee5-b049-d1cbcbff147f";
        //查询出该用户的所有相册
        JsonResult jsonResult = userService.showUserPhotoAlbum(userId);
        model.addAttribute("jsonResult",jsonResult);
        return "paul-html/showUserPhotoAlbum";
    }

    /**
     *@Description: 通过用户编号查询用户的相册信息
     *@Author: 魏泉琦
     *@date: 2019/4/9
     */
    @ResponseBody
    @PostMapping(value = "queryUserAlbumMsg")
    public JsonResult queryUserAlbumMsg(HttpServletRequest request){
      /*
        HttpSession session = request.getSession();
        TbUser user =(TbUser) session.getAttribute("user");
        JsonResult jsonResult = userService.showUserPhotoAlbum(user.getUserId());
        return jsonResult;
       */

      //测试
        return userService.showUserPhotoAlbum("97fd7504-2253-4ee5-b049-d1cbcbff147f");
    }
}
